package com.cmcc.linlaker.ui.upload.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.cmcc.linlaker.R;
import com.cmcc.linlaker.application.LinhuApp;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class ImageAdapter2 extends RecyclerView.Adapter<ImageAdapter2.ViewHolder> {

    private List<String> itemList;
    private Context mContext;

    public ImageAdapter2(Context mContext, List<String> itemList) {
        this.itemList = itemList;
        this.mContext = mContext;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.image_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        String item = itemList.get(position);


        holder.layout_select_pic.setVisibility(View.GONE);
        holder.image.setVisibility(View.VISIBLE);
        holder.layout_delete.setVisibility(View.GONE);

        ImageLoader.getInstance()
                .displayImage(item, holder.image, LinhuApp.options);

        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(position);
            }
        });
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public RelativeLayout layout_delete, layout_select_pic;
        public ImageView image;


        public ViewHolder(View itemView) {
            super(itemView);

            image = itemView.findViewById(R.id.image);
            layout_delete = itemView.findViewById(R.id.layout_delete);
            layout_select_pic = itemView.findViewById(R.id.layout_select_pic);
        }
    }

}

